package com.EAsystem.service.Impl;

import com.EAsystem.entity.College;
import com.EAsystem.mapper.CollegeMapper;
import com.EAsystem.service.CollegeService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CollegeServiceImpl implements CollegeService {

    @Autowired
    private CollegeMapper collegeMapper;

    @Override
    public PageInfo<College> findAllByPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<College> list = collegeMapper.findAllWithPage();
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<College> findByNameWithPage(College college, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<College> list = collegeMapper.findByNameWithPage(college);
        return new PageInfo<>(list);
    }

    @Override
    @Transactional
    public void save(College college) {
        String now = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date());
        college.setCreateTime(now);
        college.setUpdateTime(now);
        collegeMapper.save(college);
    }

    @Override
    @Transactional
    public void delete(Integer id) {
        collegeMapper.delete(id);
    }

    @Override
    @Transactional
    public void edit(College college) {
        String now = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date());
        college.setUpdateTime(now);
        collegeMapper.edit(college);
    }

    @Override
    @Transactional
    public void batchDelete(java.util.List<Integer> ids) {
        collegeMapper.batchDelete(ids);
    }

    @Override
    public List<College> findAll() {
        return collegeMapper.findAllWithPage();
    }

    @Override
    public College findById(Integer id) {
        return collegeMapper.findById(id);
    }
}
